/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.skin.bezel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.text.JTextComponent;

import net.miginfocom.swing.MigLayout;

import org.kommando.core.search.Result;
import org.kommando.skin.DialogBasedKommandoWindow;
import org.kommando.skin.bezel.util.BezelBackgroundPainter;
import org.kommando.skin.bezel.util.SoftClipPanel;

import com.sun.jna.examples.WindowUtils;

/**
 * @author Peter De Bruycker
 */
public class BezelKommandoWindow extends DialogBasedKommandoWindow {

    private static final Dimension ICON_SIZE = new Dimension(128, 128);

    private BezelSelectorPanel directPanel = new BezelSelectorPanel(ICON_SIZE);
    private BezelSelectorPanel actionPanel = new BezelSelectorPanel(ICON_SIZE);
    private BezelSelectorPanel indirectPanel = new BezelSelectorPanel(ICON_SIZE);
    private JLabel descriptionLabel = new JLabel();
    private Selector activeSelector = Selector.DIRECT;

    private Map<Selector, BezelSelectorPanel> selectorPanels = new HashMap<Selector, BezelSelectorPanel>();

    public BezelKommandoWindow() {
        selectorPanels.put(Selector.DIRECT, directPanel);
        selectorPanels.put(Selector.ACTION, actionPanel);
        selectorPanels.put(Selector.INDIRECT, indirectPanel);
    }

    @Override
    protected JDialog createDialog() {
        // needed for transparency
        System.setProperty("sun.java2d.noddraw", "true");

        JDialog dialog = super.createDialog();

        WindowUtils.setWindowTransparent(dialog, true);

        return dialog;
    }

    @Override
    protected JComponent createDialogContent() {
        JPanel dialogContentPanel = new SoftClipPanel(new BezelBackgroundPainter());
        dialogContentPanel.setLayout(new MigLayout("nocache, hidemode 3", "15[]15", "15[]5[]10"));

        dialogContentPanel.add(directPanel.getComponent(), "cell 0 0, gapafter 10");
        dialogContentPanel.add(actionPanel.getComponent(), "cell 0 0");
        dialogContentPanel.add(indirectPanel.getComponent(), "cell 0 0, gapbefore 10");

        descriptionLabel = new JLabel(" ", SwingConstants.CENTER);
        descriptionLabel.setForeground(Color.WHITE);
        dialogContentPanel.add(descriptionLabel, "newline, alignx center, wmax 80%");

        return dialogContentPanel;
    }

    @Override
    public void expand() {
        indirectPanel.getComponent().setVisible(true);
        getWindowControl().pack();
    }

    @Override
    public void setSelection(Selector selector, Result<?> selection) {
        selectorPanels.get(selector).setResult(selection);

        updateDescription();
    }

    @Override
    public void shrink() {
        indirectPanel.getComponent().setVisible(false);
        getWindowControl().pack();
    }

    @Override
    public Dimension getIconSize() {
        return ICON_SIZE;
    }

    @Override
    public Point getSelectionListPopupPosition(Selector selector) {
        JComponent component = selectorPanels.get(selector).getComponent();

        int x = component.getLocationOnScreen().x;
        int y = getWindowControl().getLocationOnScreen().y + getWindowControl().getHeight();

        return new Point(x, y);
    }

    @Override
    public void setActiveSelector(Selector selector) {
        this.activeSelector = selector;
        updateDescription();

        for (Map.Entry<Selector, BezelSelectorPanel> entry : selectorPanels.entrySet()) {
            entry.getValue().setSelected(selector == entry.getKey());
        }
    }

    private void updateDescription() {
        Result<?> activeResult = selectorPanels.get(activeSelector).getResult();
        descriptionLabel.setText(activeResult == null ? " " : activeResult.getDescription());
    }

    @Override
    public void setInTextMode(Selector selector, boolean inTextMode) {
        selectorPanels.get(selector).setInTextMode(inTextMode);
    }

    @Override
    public void setTextComponent(Selector selector, JTextComponent textComponent) {
        selectorPanels.get(selector).setTextComponent(textComponent);
    }
}
